package com.dk.service.impl;

import com.dk.mapper.MenuRoleMapper;
import com.dk.mapper.TabMenuMapper;
import com.dk.pojo.TabMenu;
import com.dk.service.MenuService;
import org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private TabMenuMapper tabMenuMapper;
    @Autowired
    private MenuRoleMapper menuRoleMapper;

    @Override
    public List<Map<String, Object>> selectInfo() {
        List<TabMenu> tabMenus = tabMenuMapper.selectByExample(null);
        List<Map<String, Object>> tree = Tree(tabMenus, 0);
        return tree;
    }


    public List<Map<String, Object>> Tree(List<TabMenu> list, Integer id) {
        List<Map<String, Object>> listmap = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            TabMenu tabMenu = list.get(i);
            Map<String, Object> map = null;
            if (tabMenu.getPid().equals(id)) {
                map = new HashMap();
                map.put("id", tabMenu.getMid());
                map.put("text", tabMenu.getText());
                map.put("url", tabMenu.getUrl());
                map.put("nodes", Tree(list, tabMenu.getMid()));

            }
            if (map != null) {
                listmap.add(map);
                List<Map<String, Object>> nodes = (List<Map<String, Object>>) map.get("nodes");
                if (nodes.size() <= 0) {
                    map.remove("nodes");
                }
            }
        }
        return listmap;
    }

    @Override
    public List<Map<String, Object>> selectztreeInfo() {
        List<TabMenu> tabMenus = tabMenuMapper.selectByExample(null);
        List<Map<String, Object>> zterr = getZterr(tabMenus, 0);
        return zterr;
    }


    public List<Map<String, Object>> getZterr(List<TabMenu> list, Integer id) {

        List<Map<String, Object>> listmap = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            TabMenu menu = list.get(i);
            Map<String, Object> map = null;
            if (menu.getPid() == id) {
                map = new HashMap<>();
                map.put("id", menu.getMid());
                map.put("name", menu.getText());
                map.put("children", getZterr(list, menu.getMid()));
            }
            if (map != null) {
                listmap.add(map);

                List<Map<String, Object>> children = (List<Map<String, Object>>) map.get("children");
                if (children.size() <= 0) {
                    map.remove("children");

                }

            }
        }
        return listmap;
    }

    @Override
    public boolean insertinto(TabMenu t) {
        int i = tabMenuMapper.insertSelective(t);
        return i > 0;
    }

    //回选
    @Override
    public TabMenu selectByid(Integer id) {
        TabMenu tabMenu = tabMenuMapper.selectByPrimaryKey(id);
        return tabMenu;
    }

    //修改
    @Override
    public boolean updateInfo(TabMenu t) {
        int i = tabMenuMapper.updateByPrimaryKeySelective(t);
        return i > 0;
    }

    @Override
    public boolean deleteInfo(Integer id) {
        int i = tabMenuMapper.deleteByPrimaryKey(id);
        return i > 0;
    }

    @Override
    public List<Map<String, Object>> getztreeInfo(Integer rid) {
        List<TabMenu> tabMenus = tabMenuMapper.selectByExample(null);
        List<Integer> mids = menuRoleMapper.selectByrid(rid);
        System.err.println(rid);
        System.err.println(mids);
        List<Map<String, Object>> mapList = getztreeInfoinf(tabMenus, 0, mids);
        return mapList;
    }

    public List<Map<String, Object>> getztreeInfoinf(List<TabMenu> list, Integer id, List<Integer> mids) {
        List<Map<String, Object>> listmap = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            TabMenu menu = list.get(i);
            Map<String, Object> map = null;
            if (menu.getPid() == id) {
                map = new HashMap<>();
                map.put("id", menu.getMid());
                map.put("name", menu.getText());
                if (mids.size() > 0 && mids.contains(menu.getMid()) && mids != null) {
                    map.put("checked", true);
                }
                map.put("children", getztreeInfoinf(list, menu.getMid(), mids));

            }
            if (map != null) {
                listmap.add(map);
                List<Map<String, Object>> children = (List<Map<String, Object>>) map.get("children");
                if (children.size() <= 0) {
                    map.remove("children");
                }
            }

        }
        return listmap;

    }


}